import {
  joinGroupApplyList,
  masterExamine
} from '../../apis/group'
const app = getApp();

Page({
  data: {
    applyList: [],
    pageNum: 1,
    isHaveMore: true
  },

  onLoad(options) {
    this.setData({
      ...options
    });
    if (options.type === 'group') {
      this.getApplyList({
        groupId: options.groupId,
        pageNum: 1,
        paegSize: 99999999
      });
    } else {
      this.getApplyList({
        pageNum: 1,
        paegSize: 99999999
      });
    };
  },

  onReachBottom() {
    if (this.data.isHaveMore) {
      this.setData({
        pageNum: this.data.pageNum + 1
      });
      if (this.data.type === 'group') {
        this.getApplyList({
          groupId: this.data.groupId,
          pageNum: this.data.pageNum,
          paegSize: 10
        });
      } else {
        this.getApplyList({
          pageNum: this.data.pageNum,
          paegSize: 10
        });
      };
    };
  },

  getApplyList(data = {}) {
    joinGroupApplyList(data).then(res => {
      if (res.data.code === 200) {
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          if (!new RegExp('https://').test(v.avatar) && !new RegExp('http://').test(v.avatar)) v.avatar = app.globalData.url + v.avatar;
          this.data.applyList.push(v);
        });
        this.setData({
          applyList: this.data.applyList
        });
        if (this.data.applyList.length === total) {
          this.setData({
            isHaveMore: false
          });
        };
      };
    });
  },

  action(e) {
    let {
      id,
      type,
      index
    } = e.currentTarget.dataset;
    if (type == 0) { // 拒绝
      this.masterExamine(id, 0, index);
    } else { // 同意
      this.masterExamine(id, 1, index);
    };
  },

  masterExamine(id, checked, index) {
    masterExamine(id, checked).then(res => {
      if (res.data.code === 200) {
        if (checked == 0) {
          this.data.applyList[index].checkedStatus = 0;
          this.setData({
            applyList: this.data.applyList
          });
        } else {
          this.data.applyList[index].checkedStatus = 1;
          this.setData({
            applyList: this.data.applyList
          });
        };
      };
    });
  },

  previewMemberCard(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/cardPreview/index?cardId=${id}`
    });
  },
})